package com.hd.vote.User.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hd.vote.User.entity.Candidate;
import com.hd.vote.User.entity.Vote;
import com.hd.vote.User.service.ICandidateService;
import com.hd.vote.User.service.IVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hd
 * @since 2023-04-14
 */
@RestController
@RequestMapping("vote")
public class VoteController {
    @Autowired
    ICandidateService candidateService;
    @Autowired
    IVoteService voteService;
    long date1=0;
    long date2=0;
    int num=0;
    //第2题
    @GetMapping("/find")
    public HashMap<String,Object> find(String phone){
        //返回的map
        HashMap<String,Object> map=new HashMap<>();
        //存储的暂时map的list
        List<Object> list1 = new ArrayList<>();
        QueryWrapper<Vote> wrapper = new QueryWrapper<>();
        QueryWrapper<Candidate> wrapper2 = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        //根据电话获取投票id
        List<Vote> list = voteService.list(wrapper);
        if (list!=null){
            map.put("msg","OK");
            map.put("code", 1);
            //根据id获取候选人信息
            for (int i = 0; i < list.size(); i++) {
                //暂时存储的map
                HashMap<String,Object> map1=new HashMap<>();
                wrapper2.eq("id",list.get(i).getCandidateId());
                Map<String, Object> voteServiceMap = candidateService.getMap(wrapper2);
                System.out.println(voteServiceMap);
                map1.put("phone",phone);
                map1.put("candidateId",voteServiceMap.get("id"));
                map1.put("candidateName",voteServiceMap.get("name"));
                map1.put("voteNum",voteServiceMap.get("vote_num"));
                list1.add(map1);
                System.out.println(map1);
                wrapper2.clear();
            }
            System.out.println(list1);
            map.put("date",list1);

        }else {
            map.put("msg","接口操作失败，请联系运维人员");
            map.put("code", -1);
        }
        return map;
    }
    @GetMapping("/find1")
    public HashMap<String,Object> find1(String phone) {
        //返回的map
        HashMap<String, Object> map = new HashMap<>();
        List<Object> list = voteService.find(phone);
        if (list!=null){
            map.put("msg","OK");
            map.put("code", 1);
            map.put("date",list);
        } else {
        map.put("msg","接口操作失败，请联系运维人员");
        map.put("code", -1);
    }
        return map;
    }
    //第四题
    @GetMapping("/all")
    public HashMap<String,Object> all(String phone) {
        //返回的map
        HashMap<String, Object> map = new HashMap<>();
        //存储的暂时map的list
        List<Object> list1 = new ArrayList<>();
        QueryWrapper<Candidate> wrapper = new QueryWrapper<>();
        //获取所有候选人
        List<Candidate> list = candidateService.list(wrapper);
        if (list!=null){
            map.put("msg","OK");
            map.put("code", 1);
            //根据id获取候选人信息
            for (int i = 0; i < list.size(); i++) {
                //暂时存储的map
                HashMap<String,Object> map1=new HashMap<>();
                map1.put("candidateId",list.get(i).getId());
                map1.put("candidateName",list.get(i).getName());
                map1.put("voteNum",list.get(i).getVoteNum());
                list1.add(map1);

            }
            System.out.println(list1);
            map.put("date",list1);

        }else {
            map.put("msg","接口操作失败，请联系运维人员");
            map.put("code", -1);
        }
        return map;
    }
    //第三题
    @GetMapping("/vote")
    public HashMap<String,Object> vote(String phone,String name) {
        //返回的map
        HashMap<String, Object> map = new HashMap<>();
        if (num==0){
            HashMap<String, Object> vote = voteService.vote(phone, name);
            date1=System.currentTimeMillis();
            map=vote;
            num++;
        }else {
            date2=date1;
            date1=System.currentTimeMillis();
            if (date1 - date2 < 60000) {
                map.put("msg", "接口操作失败，请联系运维人员");
                map.put("code", -1);
            } else {
                HashMap<String, Object> vote = voteService.vote(phone, name);
                map=vote;
            }
        }
        return map;
    }

}
